﻿using SpeechLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Zzj.Pages;
using Zzj.Utils;
using Zzj.Windows;

namespace Zzj
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private DispatcherTimer timer;
        private String TextProperty1 = String.Empty;
        private SpVoice speech = new SpVoice();
        private MainWindow mainWindow;

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += new EventHandler(timeCycle);
            timer.Start();

            mainWindow = this;

            this.Loaded += Window_Load;
            this.Unloaded += Window_UnLoad;
        }

        private void Window_Load(object sender, EventArgs e)
        {
            Index indexPage = new Index(this);
            IdCardRead.cardReaderInitialization();//读卡器初始化

            frmMain.Content = indexPage;
        }

        private void Window_UnLoad(object sender, EventArgs e)
        {
            IdCardRead.turnOffCardReader();//读卡器关闭
        }

        public void Speak(String message)
        {
            speech.Speak(message, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }

        public void BreakSpeak()
        {
            speech.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }

        public void timeCycle(object sender, EventArgs e)
        {
            new Thread(() =>
            {
                Application.Current.Dispatcher.BeginInvoke(
                    new Action(() =>
                    {
                        TextProperty1 = DateTime.Now.ToString();
                        this.labelTime.Content = TextProperty1;
                    }));
            }).Start();
        }

        private void imgLogo_MouseUp(object sender, MouseButtonEventArgs e)
        {
            PasswordWindow passwordWindow = new PasswordWindow();
            passwordWindow.Owner = mainWindow;
            bool result = (bool)passwordWindow.ShowDialog();
            if (!result)
            {
                return;
            }

            SettingWindow win = new SettingWindow();
            win.Owner = mainWindow;
            result = (bool)win.ShowDialog();
            if (result)
            {
                Application.Current.Shutdown();
            }
        }
    }
}